package com.dankegongyu.lib;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.dankegongyu.lib.base.BaseAppActivity;
import com.dankegongyu.lib.common.util.DKLog;
import com.dankegongyu.lib.common.util.DKMediaUtil;
import com.dankegongyu.lib.common.util.DKNotificationUtils;
import com.dankegongyu.lib.common.util.DKPermission;
import com.dankegongyu.lib.common.util.DKToast;
import com.dankegongyu.lib.dropdownmenu.DropDownMenuActivity;
import com.dankegongyu.lib.expandtextview.TestExpandTextActivity;
import com.dankegongyu.lib.list.TestHorizontalWrappedListViewActivity;
import com.dankegongyu.lib.recyclerview.TestRecyclerActivity;

import java.io.File;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseAppActivity {

    public static final int REQUEST_CODE_PHOTO = 1000;
    public static final int REQUEST_CODE_CROP = 1001;
    public static final int REQUEST_CODE_NOTIFICATION = 1002;

    @BindView(R.id.ll_content)
    LinearLayout mLlContent;
    @BindView(R.id.btn_dropDownMenu)
    Button mBtnDropDownMenu;
    @BindView(R.id.sdv_avatar)
    ImageView mSdvAvatar;

    @Override
    public int getContentViewLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void init(@Nullable Bundle savedInstanceState) {
        String[] permissions = {
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.ACCESS_COARSE_LOCATION, // 定位
                Manifest.permission.ACCESS_FINE_LOCATION};
        DKPermission.request(this, permissions, new DKPermission.Callback() {
            @Override
            public void onResult(boolean granted) {
                checkNotification();
            }
        });
//        LoadingDialog loadingDialog = LoadingDialog.show(this);
//        if (loadingDialog != null && loadingDialog.getWindow() != null) {
//            loadingDialog.getWindow().setDimAmount(0.7F);
//        }
        Glide.with(this)
                .load("https://public.danke.com.cn/public-20180102-FtIDnzAcVND6r4Spiy2kIBEVX1qn")
                .transform(new CircleCrop())
                .into(mSdvAvatar);
//        FrescoImageHelper.getImageForMiddle("https://public.danke.com.cn/public-20180102-FtIDnzAcVND6r4Spiy2kIBEVX1qn", mSdvAvatar);

//        DKPermission.requestLocationPermission(this, new DKPermission.Callback() {
//            @Override
//            public void onResult(boolean granted) {
//                DKToast.show("请求定位权限：" + granted);
//            }
//        });

//        DKPermission.requestPhonePermission(this, new DKPermission.Callback() {
//            @Override
//            public void onResult(boolean granted) {
////                DKToast.show("请求电话权限：" + granted);
//            }
//        });
//
//        DKPermission.requestCameraPermission(this, new DKPermission.Callback() {
//            @Override
//            public void onResult(boolean granted) {
//                DKToast.show("请求摄像头和存储权限：" + granted);
////                DKFileUtil.createAppDir("Download");
//            }
//        });

//        DKPermission.requestStoragePermissions(this, new DKPermission.Callback() {
//            @Override
//            public void onResult(boolean granted) {
//                DKToast.show("请求存储权限：" + granted);
//            }
//        });
    }

    private void checkNotification() {
        if (!DKNotificationUtils.areNotificationsEnabled(this)) {
            new AlertDialog.Builder(this)
                    .setTitle("通知提醒")
                    .setMessage("没有开启通知，点确定去开启")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            DKNotificationUtils.openNotificationSetting(MainActivity.this);
                        }
                    })
                    .create().show();
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        DKToast.show("通知是否已开启：" + DKNotificationUtils.areNotificationsEnabled(this), Gravity.CENTER);
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void doBusiness() {

    }

    @OnClick({R.id.btn_dropDownMenu, R.id.btn_expand_text, R.id.btn_wrapList, R.id.sdv_avatar, R.id.btn_recycler})
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.btn_dropDownMenu:
                startActivity(new Intent(this, DropDownMenuActivity.class));
                break;
            case R.id.btn_expand_text:
                startActivity(new Intent(this, TestExpandTextActivity.class));
                break;
            case R.id.btn_wrapList:
                startActivity(new Intent(this, TestHorizontalWrappedListViewActivity.class));
                break;
            case R.id.btn_recycler:
                startActivity(new Intent(this, TestRecyclerActivity.class));
//                PhoneUtil.call(this, "18234096324");
                break;
            case R.id.sdv_avatar:
//                DKMediaUtil.startCamera(this, getPackageName() + ".fileprovider", REQUEST_CODE_PHOTO);
//                DKMediaUtil.selectPics(this, getPackageName() + ".fileprovider", 9, 666);
                DKMediaUtil.startCamera(this, getPackageName() + ".fileprovider", REQUEST_CODE_PHOTO);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        DKLog.e("onActivityResult");
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQUEST_CODE_PHOTO:
                    DKLog.e("photoUri.toString(): " + DKMediaUtil.getPhotoUri().toString()
                            + "\n" + "photoUri.getPath(): " + DKMediaUtil.getPhotoUri().getPath()
                            + "\n" + "photoPath: " + DKMediaUtil.getPhotoPath());
//                    mSdvAvatar.setImageURI(DKMediaUtil.getPhotoUri());
//                    mSdvAvatar.setImageURI(DKMediaUtil.getPhotoUri().toString());
                    DKMediaUtil.refreshGallery(this, new File(DKMediaUtil.getPhotoPath()));
                    DKMediaUtil.cropImage(this, DKMediaUtil.getPhotoUri(), getPackageName() + ".fileprovider", REQUEST_CODE_CROP);
                    break;
                case REQUEST_CODE_CROP:
                    DKLog.e("cropUri.toString(): " + DKMediaUtil.getCropUri().toString()
                            + "\n" + "cropUri.getPath(): " + DKMediaUtil.getCropUri().getPath()
                            + "\n" + "cropPath: " + DKMediaUtil.getCropPath());
                    mSdvAvatar.setImageURI(DKMediaUtil.getCropUri());
                    break;
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
//        DKLog.e(ClipboardUtils.getClipboardContent(this));
    }

    @Override
    protected void setCustomStatusBar() {
        super.setCustomStatusBar();
//        DKStatusBarUtils.setTranslucentForImageViewLightMode(this, 0, null);
    }
}
